package org.torreya.etange;

import java.sql.Connection;
import java.sql.DriverManager;

public class DataSource {

	private String driver;
	private String dbName;
	private String dbURL;
	private String username;
	private String Password;
	private String useUnicode;
	private String characterEncoding;

	/**
	 * constructor method
	 * 
	 * @param driver
	 * @param dbName
	 * @param dbURL
	 * @param username
	 * @param password
	 * @param useUnicode
	 * @param characterEncoding
	 */
	public DataSource(String driver, String dbName, String dbURL,
			String username, String password, String useUnicode,
			String characterEncoding) {
		super();
		this.driver = driver;
		this.dbName = dbName;
		this.dbURL = dbURL;
		this.username = username;
		this.Password = password;
		this.useUnicode = useUnicode;
		this.characterEncoding = characterEncoding;
	}

	/**
	 * double thread method,create a new connection
	 * 
	 * @return connection
	 * @throws Exception
	 */
	public synchronized Connection createConnection() throws Exception {
		Connection connection = null;
		// find driver by driver name
		Class.forName(driver).newInstance();
		// initialize parameters by useUnicode and characterEncoding
		StringBuffer params = new StringBuffer("");
		if (useUnicode != null && !useUnicode.equals(""))
			params.append("?useUnicode=" + useUnicode);
		if (characterEncoding != null && !characterEncoding.equals("")) {
			if (!params.equals(""))
				params.append("&characterEncoding=" + characterEncoding);
			else
				params.append("?characterEncoding=" + characterEncoding);
		}
		// create a new connection by url, username, password
		connection = DriverManager.getConnection(dbURL + params.toString(),
				username, Password);
		return connection;
	}

	/**
	 * @return the driver
	 */
	public String getDriver() {
		return driver;
	}

	/**
	 * @param driver
	 *            the driver to set
	 */
	public void setDriver(String driver) {
		this.driver = driver;
	}

	/**
	 * @return the dbName
	 */
	public String getDbName() {
		return dbName;
	}

	/**
	 * @param dbName
	 *            the dbName to set
	 */
	public void setDbName(String dbName) {
		this.dbName = dbName;
	}

	/**
	 * @return the dbURL
	 */
	public String getDbURL() {
		return dbURL;
	}

	/**
	 * @param dbURL
	 *            the dbURL to set
	 */
	public void setDbURL(String dbURL) {
		this.dbURL = dbURL;
	}

	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * @param username
	 *            the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return Password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		Password = password;
	}

	/**
	 * @return the useUnicode
	 */
	public String getUseUnicode() {
		return useUnicode;
	}

	/**
	 * @param useUnicode
	 *            the useUnicode to set
	 */
	public void setUseUnicode(String useUnicode) {
		this.useUnicode = useUnicode;
	}

	/**
	 * @return the characterEncoding
	 */
	public String getCharacterEncoding() {
		return characterEncoding;
	}

	/**
	 * @param characterEncoding
	 *            the characterEncoding to set
	 */
	public void setCharacterEncoding(String characterEncoding) {
		this.characterEncoding = characterEncoding;
	}

}
